<?php
//**---------------------------------------------------------------------+
//** 服务层文件 -- 余额充值
//**---------------------------------------------------------------------+
//** 版权所有：江西佰商科技有限公司. 官网：https://www.tigshop.com
//**---------------------------------------------------------------------+
//** 作者：Tigshop团队，yq@tigshop.com
//**---------------------------------------------------------------------+
//** 提示：Tigshop商城系统为非免费商用系统，未经授权，严禁使用、修改、发布
//**---------------------------------------------------------------------+

namespace app\service\admin\promotion;

use app\model\promotion\RechargeSetting;
use app\service\common\BaseService;
use exceptions\ApiException;

/**
 * 余额充值服务类
 */
class RechargeSettingService extends BaseService
{
    public function __construct()
    {
    }

    /**
     * 获取筛选结果
     *
     * @param array $filter
     * @return array
     */
    public function getFilterResult(array $filter): array
    {
        $query = $this->filterQuery($filter);
        $result = $query->page($filter['page'], $filter['size'])->select();
        return $result->toArray();
    }

    /**
     * 获取筛选结果数量
     *
     * @param array $filter
     * @return int
     */
    public function getFilterCount(array $filter): int
    {
        $query = $this->filterQuery($filter);
        $count = $query->count();
        return $count;
    }

    /**
     * 筛选查询
     *
     * @param array $filter
     * @return object
     */
    public function filterQuery(array $filter): object
    {
        $query = RechargeSetting::query();
        // 处理筛选条件

        if (isset($filter['sort_field'], $filter['sort_order']) && !empty($filter['sort_field']) && !empty($filter['sort_order'])) {
            $query->order($filter['sort_field'], $filter['sort_order']);
        }
        return $query;
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return RechargeSetting
     * @throws ApiException
     */
    public function getDetail(int $id): RechargeSetting
    {
        $result = RechargeSetting::where('recharge_id', $id)->find();

        if (!$result) {
            throw new ApiException(/** LANG */'余额充值不存在');
        }

        return $result;
    }

    /**
     * 添加余额充值
     * @param array $data
     * @return int
     */
    public function createRechargeSetting(array $data): int
    {
        $result = RechargeSetting::create($data);
        return $result->getKey();
    }

    /**
     * 执行余额充值更新
     *
     * @param int $id
     * @param array $data
     * @return bool
     * @throws ApiException
     */
    public function updateRechargeSetting(int $id, array $data): bool
    {
        if (!$id) {
            throw new ApiException(/** LANG */'#id错误');
        }
        $result = RechargeSetting::where('recharge_id', $id)->save($data);
        return $result !== false;
    }

    /**
     * 更新单个字段
     *
     * @param int $id
     * @param array $data
     * @return int|bool
     * @throws ApiException
     */
    public function updateRechargeSettingField(int $id, array $data)
    {
        if (!$id) {
            throw new ApiException(/** LANG */'#id错误');
        }
        $result = RechargeSetting::where('recharge_id', $id)->save($data);
        return $result !== false;
    }

    /**
     * 删除余额充值
     *
     * @param int $id
     * @return bool
     */
    public function deleteRechargeSetting(int $id): bool
    {
        if (!$id) {
            throw new ApiException(/** LANG */'#id错误');
        }
        $result = RechargeSetting::destroy($id);
        return $result !== false;
    }
}
